
let PermissionHelper={

  /**
   * 验证permissionCollection 中是否包含所有 validPermissions
   * @param permissionCollection
   * @param validPermissions
   */
  containsAllPermissions: (permissionCollection , validPermissions)=>{
    if(validPermissions ==null ) {
      return false;
    }

    // permissionCollection 必须是数组
    if(!Array.isArray(permissionCollection)){
      throwError('permissionCollection is not Array!');
      return false;
    }

    // 要求必须是数组或字符串
    if(!Array.isArray(validPermissions) && (typeof validPermissions != "string")){
      return false;
    }

    // 如果是字符串
    if(typeof validPermissions == 'string'){
      return this.hasPermission(permissionCollection,validPermissions);
    }
    else if(Array.isArray(validPermissions)){

      // 循环遍历
      for(let i in validPermissions){

        let contains = false;
        let validPerm = validPermissions[i];
        contains = this.hasPermission(permissionCollection,validPerm);
        if(!contains){
          return false;
        }
      }
      return true;

    }

    return false;

  },
  /**
   * 包含一个 validPermission即可
   * @param permissionCollection
   * @param validPermission
   * @returns {boolean}
   */
  containsAnyPermissions: (permissionCollection , validPermission)=>{
    if(validPermissions ==null ) {
      return false;
    }

    // permissionCollection 必须是数组
    if(!Array.isArray(permissionCollection)){
      throwError('permissionCollection is not Array!');
      return false;
    }

    // 要求必须是数组或字符串
    if(!Array.isArray(validPermissions) && (typeof validPermissions != "string")){
      return false;
    }

    // 如果是字符串
    if(typeof validPermissions == 'string'){
      return this.hasPermission(permissionCollection,validPermissions);
    }
    // 如果是数组
    else if(Array.isArray(validPermissions)){

      // 循环遍历, 只要包含一个，则返回true
      for(let i in validPermissions){

        let contains = false;
        let validPerm = validPermissions[i];
        contains = this.hasPermission(permissionCollection,validPerm);
        if(contains){
          return true;
        }
      }
      return false;

    }

    return false;
  },
  hasPermission:(permissionCollection , validPermission)=>{
    if(!Array.isArray(permissionCollection)|| typeof validPermission !='string'){
      return false;
    }
    let contains = false;
    for(let i in permissionCollection){
      let perm = permissionCollection[i];
      if(perm == validPermission){
        contains = true;
        break;
      }
    }
    return contains;
  },


}

export default PermissionHelper;
